package com.bolt.admin.module.sys.entity;

import com.bolt.support.spring.jpa.annotation.QueryBuild;
import com.bolt.support.spring.jpa.entity.BaseUuidAuditingEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-09-10 15:25:48
 *         用户信息
 **/
@Access(AccessType.FIELD)
@Entity
@Table(name = "sys_user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
@QueryBuild
public class UserEntity extends BaseUuidAuditingEntity {

    public UserEntity() {

    }

    public UserEntity(String id) {
        setId(id);
    }

    /**
     * 账号类型所对应唯一标识
     */
    @Column(name = "USER_NAME", length = 64)
    private String userName;


//    public ColumnClause<UserEntity> userName(){
//       return  Clauses.of("",UserEntity.class);
//    }

    /**
     * 姓名
     */
    @Column(name = "REAL_NAME", length = 128)
    private String realName;

    /**
     * 昵称
     */
    @Column(name = "NICK_NAME", length = 128)
    private String nickName;

    /**
     * 用户的密码
     */
    @Column(name = "PASSWORD", length = 64)
    private String password;

    /**
     *
     */
    @Column(name = "EMAIL", length = 100)
    private String email;

    /**
     * 手机号
     */
    @Column(name = "MOBILE", length = 19)
    private String mobile;

    /**
     * 修改密码的时间
     */
    @Column(name = "PWD_RESET_TIME", length = 19)
    private Date pwdResetTime;

    /**
     * 状态：1启用、0禁用
     */
    @Column(name = "ENABLED", length = 3)
    private Boolean enabled;

    /**
     * 是否为admin账号
     */
    @Column(name = "IS_ADMIN", length = 3)
    private Boolean admin = false;

    /**
     * 用户登录密码的SALT
     */
    @Column(name = "AUTH_SALT", length = 128)
    private String authSalt;

    /**
     * 机构ID
     */
    @Column(name = "ORG_ID", length = 36, updatable = false, insertable = false)
    private String orgId;


    /**
     * 头像地址
     */
    @Column(name = "AVATAR_PATH", length = 255)
    private String avatarPath;


    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "ORG_ID")
    private OrgEntity orgInfo;


    @ManyToMany
    @JoinTable(name = "SYS_USER2ROLE",
            joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
    @JsonIgnore
    private List <RoleEntity> roles;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    @JsonIgnore
    private List <UserR2Role> userR2Roles;


}